﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.GZip; 
using ICSharpCode.SharpZipLib.Zip; 
using System.IO;
using System;






public enum SAAttributeType
{
	NONE = 0, // 无
	
	EARTH, // 地
	WATER, // 水
	FIRE, // 火
	WIND, // 风
	
	ALL, // 全
	
	COUNT
}



public enum SANormalStatusType
{
	NORMAL = 0,
	DEFENCE,
	
	FALL,
	COUNT
};


public enum SABattleStatusType
{
	NONE = 0,
	ALL = 1,  
	POISON = 2,  // 毒
	PARALYSIS = 3, // 麻痹
	STONE = 4, // 石化
	CONFUSION = 5, // 混乱
	DRUNK = 6, // 酒醉
	SLEEP = 7, // 睡眠
	
	TOXIC = 8, // 猛毒
	BARRIER = 9, // 壁障
	SILENCE = 10, // 沉默
	WEAK = 11 , // 虚弱
	DOWN = 12 , // 落马
	
	COUNT
}



public enum SAMaterialType
{
	ROCK = 0, // 石
	WOOD = 1, // 木
	SKIN = 2, // 皮
	BONE = 3, // 骨
	THREAD = 4, // 线
	SCALE = 5, // 鳞
	LEAF = 6, // 叶
	LAND = 7, // 土
	CONCH = 8, // 贝
	SHELL = 9, // 壳
	CLAW = 10, // 爪
	CRYSTAL = 11, // 水晶
	FLOWER = 12, // 花
	TOOTH = 13, // 牙
	CLAY = 14, // 黏土
	
	POISON = 20, // 毒
	PARALYSIS = 21, // 麻痹
	STONE = 22, // 石化
	CONFUSION = 23, // 混乱
	DRUNK = 24, // 酒醉 
	SLEEP = 25, // 睡眠
	CURE = 26, // 回复
	RELIVE = 27, // 复活
	
	LOYALA = 30, // 忠A
	LOYALB = 31, // 忠B
	
	
	EARTH = 50, // 地
	WATER = 51, // 水
	FIRE = 52, // 火
	WIND = 53, // 风
	
	ACORN = 100, // 橡树果
	BEANS = 101, // 豆
	CARBON = 102, // 碳
	SUGAR = 103, // 砂糖
	VANILLA = 104, //香草
	MEAT = 105, // 肉
	FISH = 106, // 鱼
	SKEWERS = 107, // 串
	EGG = 108, // 卵
	DRINKWATER = 109, // 饮用水
	SALT = 110, // 盐
	SHRIMP = 111, // 虾
	CRAB = 112, // 螃蟹
	OCTOPUS = 113, // 章鱼
	GARLIC = 114, // 大蒜
	ONIONS = 115, // 葱
	PEPPER = 116, // 胡椒
	MUSHROOM = 117, // 香菇
	RICE = 118, // 米
	SEAWEED = 119, // 海草
	APPLE = 120, // 苹果
	LIZARD = 121, // 蜥蜴
	STARFISH = 122, // 海星
	FROG = 123, // 青蛙
	GINGER = 124, // 生姜
	ASPARAGUS = 125, // 芦笋
	CARROTS = 126, // 红萝卜
	POTATO = 127, // 马铃薯
	PEAR = 128, // 梨
	CHERRY = 129, // 樱桃
	BAMBOO = 130, // 竹笋
	GRAPES = 131, // 葡萄
	CEREALS = 132, // 谷
	CABBAGE = 133, // 包心菜
	CHILI = 134, // 青椒
	TOMATOE = 135, // 番茄
	EGGPLANT = 136, // 茄子
	CUCUMBER = 137, // 小黄瓜
	PEACH = 138, // 桃子
	STRAWBERRY = 139, // 草莓
	OIL = 140, // 油
	ORANGE = 141, // 橘子
	PERSIMMON = 142, // 柿子
	CHINESECABBAGE = 143, // 白菜
	PUMPKIN = 144, // 南瓜
	MSG = 145, // 味精
	SOUPSTOCK = 146 , // 高汤
	
	IRON = 200, // 铁
	COPPER = 201, // 铜
	DIAMOND = 202, // 钻石
	RUBY = 203, // 红宝石
	SAPPHIRE = 204, // 蓝宝石
	EMERALD = 205, // 绿宝石
	TOPAZ = 206, // 黄宝石
	PEARL = 207, // 珍珠
	
	SP0 = 300, // 恒冰结晶
	SP1 = 301, // 暴龙的尖牙
	SP2 = 302, // 蝎子的刺
	SP3 = 303, // 巨象的骨头
	SP4 = 304, // 海龙的骨头
	SP5 = 305, // 龙蜥的皮
	SP6 = 306, // 雷龙的骨头
	SP7 = 307, // 老虎的骨头
	SP8 = 308, // 人鱼的皮
	
	ALCHEMY = 309, // 炼金粉末
	
	SP20 = 320, // 雷尔一
	SP21 = 321, // 雷尔二
	SP22 = 322, // 雷尔三
	SP23 = 323, // 海盗一
	SP24 = 324, // 海盗二
	SP25 = 325, // 海盗三
	SP26 = 326, // 奶牛一
	SP27 = 327, // 奶牛二
	SP28 = 328, // 奶牛三
	SP29 = 329, // 忍者一
	SP30 = 330, // 忍者二
	SP31 = 331, // 忍者三
	SP32 = 332, // 绅士一
	SP33 = 333, // 绅士二
	SP34 = 334, // 绅士三
	
	
}

public class GameDefine 
{
	public const float	GAME_VERSION = 3.01f;

	public const int	INVALID_ID = -1;

	public const int	MAX_COLOR = 256;

	public const int	MAX_ITEM = 15;
	
	public const int	MAX_STONE = 1000000;
	
	public const int	DEFAULT_JOINT_ATTACK = 20;
	
	public const int	MAX_LEVELUP_POINT = 3;
	public const int	MAX_LEVEL_CACHE = 256;
	
	public const int	MAX_TEAM_CREATURE = 5;
	public const int	MAX_TEAM_PET = 5;

	
	public const int	MAX_BATTLE_PLAYER = 5;
	public const int	MAX_BATTLE_ENEMY = 10;
	public const int	MAX_BATTLE_TURN = 30;

	public const int	MAX_RANDOM_ENEMY = 100;
	
	public const int	MAX_NPC_DATA = 1024;
	
	public const int	MAX_CHAT_MSG = 128;
	public const int	MAX_NAME = 32;
	public const int	MAX_MOVE_PATH = 128;

	public const int	MAX_MAP_PLAYER = 32;
	public const int	MAX_MAP_NPC = 32;

	public const int	MAX_MOVE_ERROR = 3;
	
	public const float	DEFAULT_MOVE_SPEED = 200.0f;
	public const float	DEFAULT_BATTLE_MOVE_SPEED = 500.0f;
	public const float	DEFAULT_BATTLE_MOVE_SPEED1 = 80.0f;
	
	public const int	MAX_ROLE_BASESTATUS = 20;
	public const int	MAX_ROLE_ATTR = 10;
	
	public const int	MAX_ROLE_COUNT = 11;

	
	
	public static float getZ( float posY )
	{
		return ( posY + 100000.0f ) / 100000.0f * 10.0f;
	}
	

	public static Transform getTransform( Transform check , string name )   
	{   
		for ( int i = 0 ; i < check.childCount ; i++ )
		{
			Transform t = check.GetChild( i );
			if ( t.name == name )
			{
				return t;
			}
			
			if ( t.childCount != 0 )
			{
				Transform t1 = getTransform( t , name );
				
				if ( t1 ) 
				{
					return t1;
				}
			}
		}
		
		return null;   
	}  

	
	public static byte[] Compress( byte[] bytesToCompress ) 
	{ 
		byte[] rebyte = null; 
		MemoryStream ms = new MemoryStream(); 
		
		GZipOutputStream s = new GZipOutputStream( ms ); 
		s.Write( bytesToCompress , 0 , bytesToCompress.Length ); 

		rebyte = ms.ToArray(); 

		s.Close(); 
		ms.Close(); 

		return rebyte;
	}
	
	public static byte[] DeCompress( byte[] bytesToDeCompress ) 
	{ 
		byte[] rebyte = new byte[ bytesToDeCompress.Length * 20 ];

		MemoryStream ms = new MemoryStream( bytesToDeCompress ); 
		MemoryStream outStream = new MemoryStream();


		GZipInputStream s = new GZipInputStream( ms ); 
		int read = s.Read( rebyte , 0 , rebyte.Length ); 
		while ( read > 0 )
		{
			outStream.Write( rebyte, 0 , read );
			read = s.Read( rebyte , 0, rebyte.Length );
		}

		byte[] rebyte1 = outStream.ToArray(); 

		ms.Close();
		s.Close();
		outStream.Close();

		return rebyte1;
	}

//	public static T DeepCopy< T >( T item )
//	{
//		BinaryFormatter formatter = new BinaryFormatter();
//		MemoryStream stream = new MemoryStream();
//		formatter.Serialize( stream, item );
//		stream.Seek( 0 , SeekOrigin.Begin );
//		T result = ( T )formatter.Deserialize( stream );
//		stream.Close();
//		return result;
//	}

	
	
	public static byte[] structToBytes( object structObj )
	{
		int size = Marshal.SizeOf( structObj );
		IntPtr buffer = Marshal.AllocHGlobal( size );
		
		try
		{
			Marshal.StructureToPtr( structObj , buffer , false );
			byte[] bytes = new byte[ size ];
			Marshal.Copy( buffer , bytes , 0 , size );
			return bytes;
		}
		finally
		{
			Marshal.FreeHGlobal( buffer );
		}
	}


	public static object bytesToStruct( byte[] bytes , int index , Type strcutType )
	{
		int size = Marshal.SizeOf( strcutType );
		IntPtr buffer = Marshal.AllocHGlobal( size );

		try
		{
			Marshal.Copy( bytes , index , buffer , size );
			object obj = Marshal.PtrToStructure( buffer , strcutType );
			return obj;
		}
		finally
		{
			Marshal.FreeHGlobal( buffer );
		}
	}


	public static T[] bytesToStructArray<T>( byte[] bytes , int index , Type strcutType , int sizeT )
	{
		int size = Marshal.SizeOf( strcutType );

		T[] obj = new T[ sizeT ];

		for ( int i = 0; i < sizeT ; i++ ) 
		{
			obj[ i ] = (T)bytesToStruct( bytes , index + i * size , strcutType );
		}

		return obj;
	}


	public static void resetUICamera()
	{
		Camera uiCamera = GameObject.FindWithTag( "UICamera" ).camera;
		if( GameSetting.activeRes.type == (int)GameSetting.GameResType.RESPAD3 )
			uiCamera.orthographicSize = 0.5f;
	}

}
